<?php
/*
 * Created on Apr 2, 2006
 *
 */
 	require_once('config.php');
	require_once('functions/general.php');
	require_once('functions/sql.php');
	require_once('sql/sql_incident.php');
	require_once('sql/sql_profile.php');
	require_once('sql/sql_search.php');
	require_once('sql/sql_offender.php');
	require_once('Smarty.class.php');
	
	include_once('alien/Text_Diff/Diff.php');
	include_once('alien/Text_Diff/Diff/Renderer.php');
	include_once('alien/Text_Diff/Diff/Renderer/unified.php');
	
	
	session_start();
	$smarty = new Smarty;
	$smarty->assign('CONFIG', $CONFIG);
	
	logged_in_status($smarty);
	$mysql_link = get_mysql_link();
	$incident = new Sql_incident($mysql_link);
	
	if ($_POST) {
		//view a map with selected events.
		$data = Array();
		//fetch all data into $data
		foreach($_POST as $id) {
			$i = $incident->get_incident($id);
			if ($incident)
				$data[] = $i;
		}
		$smarty->assign('data', $data);
		$smarty->display('view_map.tpl');
	}

	else if ($_GET[incident]) {
		$incident = $incident->get_incident($_GET[incident]);
		if ($incident)
			$data[] = $incident;
		
		$smarty->assign('data', $data);
		$smarty->display('view_map.tpl');
	}
	
	else if ($_GET[history]) {
		$offender = new Sql_offender($mysql_link);
		
		
		if ($_GET[is_id]) {
			$offender_plate = $offender->get_offender_from_description($_GET[history]);
			$smarty->assign('history', $offender->get_description_history($offender_plate));
			$smarty->assign('offender', $offender_plate);
			$smarty->assign('description', $offender->get_description($_GET[history], 'is_id'));
		}
		
		else {
			if (!$offender->exists($_GET[history]))
				die ("Offender does not exist.");
			$smarty->assign('history', $offender->get_description_history($_GET[history]));
			$smarty->assign('offender', $_GET[history]);
			
		}
		
		if ($_GET[compare1] and $_GET[compare2]) {
			$description1 = $offender->get_description($_GET[compare1], true);
			$description2 = $offender->get_description($_GET[compare2], true);
			$diff = &new Text_Diff('auto', Array(
				explode("\n", $description1[description]),
				explode("\n", $description2[description])));
			
			$renderer = &new Text_Diff_renderer_unified();
			$renderer = str_replace("\n", "<br />", $renderer->render($diff));
			$smarty->assign('diff', $renderer);
		}
		
		$smarty->display('view_history.tpl');
	}
	
	else {
		$smarty->assign('title', "Ingenting å vise frem.");
		$smarty->assign('message', "Mottok ingenting å vise frem.");
		$smarty->display('warning.tpl');
	}
	
 
?>
